package com.yumeng.framework.core.mvc;

import com.yumeng.common.api.ApiResult;
import com.yumeng.common.api.IResultData;
import com.yumeng.common.api.PageData;
import com.yumeng.common.exception.YumengException;
import com.yumeng.common.utils.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.lang.reflect.Type;
import java.util.Collection;

/**
 * 全局响应处理
 *
 * @author wxd
 * @date 2024/6/5 上午11:24
 */
@Slf4j
public abstract class BaseGlobalResponseBodyAdvice implements ResponseBodyAdvice<Object> {

    /**
     * 返回false，不执行beforeBodyWrite
     * @param returnType the return type
     * @param converterType the selected converter type
     * @return
     */
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        //部分类型不需要处理，直接返回
        Class<?> paramType = returnType.getParameterType();
        boolean b = !paramType.equals(ApiResult.class)
                && !paramType.equals(ResponseEntity.class)
                && !paramType.equals(String.class);
        if (!b){
            if (log.isDebugEnabled()) {
                log.debug("当前响应类型：{}，无需处理", paramType);
            }
        }
        return b;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if (body == null){
            return ApiResult.failMsg("无响应数据");
        }
        Type type = returnType.getParameterType();
        if (log.isDebugEnabled()) {
            log.debug("当前响应类型：{}", type);
        }
        if (Boolean.class.equals(type) || boolean.class.equals(type)) {
            boolean bBody = (Boolean)body;
            if (bBody){
                return ApiResult.success();
            }else {
                return ApiResult.fail();
            }
        }else if (PageData.class.equals(type)){
            return ApiResult.successPage(body);
        }else if (body instanceof IResultData || body instanceof Collection){
            return ApiResult.successData(body);
        }else if (String.class.equals(type)){
            //无法直接处理String，需转化。 StringHttpMessageConverter 无法处理 ApiResult
            return JacksonUtils.toJsonString(ApiResult.successData(body));
        }else{
            if (log.isDebugEnabled()){
                log.debug("getParameterType():{}", returnType.getParameterType());
                log.debug("getGenericParameterType():{}", returnType.getGenericParameterType());
                log.debug("selectedConverterType():{}", selectedConverterType);
            }
            throw new YumengException("返回类型解析异常");
        }
    }
}
